CKEDITOR.dialog.add('simpledata', function(editor) {
	return {
		title: '插入字符',
		minWidth: 700,
		minHeight: 480,
		contents: [{
			id: 'tab-basic',
			label: 'Basic Settings',
			elements: [{
				type: 'radio',
				id: 'backType',
				style: 'width:20px',
				label: '返回类型',
				items: [
					['单值', 'signValue'],
					['表格格点', 'table'],
					['查询参数', 'smartParam']
				],
				'default': 'signValue',
				setup: function(widget) {
					var backType = widget.element.getAttribute('backtype');
					if (backType) {
						this.setValue(backType);
					} else {
						this.setValue('signValue');
					}
				},
				commit: function(widget) {
					var element = widget.element;
					element.setAttribute('backtype', this.getValue());
				},
				onChange: function() {
					var dataPointsContent = this.getDialog().getContentElement('tab-basic', 'dataPoints');
					var dataPointsArea = dataPointsContent.getElement();
					var val = this.getValue();
					if (val == 'table') {
						dataPointsArea.show();
					} else {
						dataPointsArea.hide();
					}
					// var dialog = this.getDialog();
					// SimpleDataHandler.move(dialog, val);
				}
			}, {
				type: 'text',
				id: 'dataPoints',
				label: '值坐标',
				style: 'display:none;width:100px',
				setup: function(widget) {
					var dataPoints = widget.element.getAttribute('datapoints');
					this.setValue(dataPoints);
				},
				commit: function(widget) {
					var dialog = this.getDialog();
					if (dialog.getContentElement('tab-basic', 'backType').getValue() == 'table') {
						widget.element.setAttribute("datapoints", this.getValue());
					}
				}
			}, {
				type: 'hbox',
				widths: ['80%', '20%'],
				children: [{
					type: 'text',
					id: 'code',
					label: '脚本',
					validate: CKEDITOR.dialog.validate.notEmpty("脚本不能为空"),
					setup: function(widget) {
						var code = widget.element.getAttribute('code');
						this.setValue(code);
					},
					commit: function(widget) {
						widget.element.setAttribute("code", this.getValue());
					}
				}, {
					id: 'preview',
					type: 'button',
					label: '预览',
					style: 'margin-top:14px',
					onClick: function() {
						var dialog = this.getDialog();
						var codeEle = dialog.getContentElement('tab-basic', 'code');
						var backTypeEle = dialog.getContentElement('tab-basic', 'backType');

						SimpleDataHandler.isChange = codeEle.isChanged();
						var code = codeEle.getValue();
						var backType = backTypeEle.getValue();
						SimpleDataHandler.preview(backType, code);
					}
				}]
			}, {
				type: 'html',
				html: '<div id="preview"><b>预览:</b><br/><div id="previewTable" style="width:700px;height:330px;border:1px solid rgb(138, 134, 134);overflow:auto;"></div></div>'
			}]
		}],
		onLoad: function() {
			$('#previewTable').on('mouseenter', 'table td', function() {
				$(this).css('background-color', 'rgb(191, 215, 248)');
			});
			$('#previewTable').on('mouseleave', 'table td', function() {
				$(this).removeAttr('style');
			});
		},
		onShow: function() {
			$('#previewTable table').remove();

			var dataPointsArea = this.getContentElement('tab-basic', 'dataPoints').getElement();
			dataPointsArea.hide();

			SimpleDataHandler.isChange = false;
			var editor = this.getParentEditor(),
				sel = editor.getSelection(),
				element = sel && sel.getSelectedElement();
			if (element) {
				element = element.findOne('.h-card');
				var code = element.getAttribute('code');
				var dataTypeVal = element.getAttribute('backType');

				if (dataTypeVal == 'table') {
					dataPointsArea.show();
				}
				SimpleDataHandler.id = element.getAttribute('id');
				SimpleDataHandler.preview(dataTypeVal, code);
				this.insertMode = false;
			} else {
				SimpleDataHandler.id = ck.util.randomString(16, 'aA');
				this.insertMode = true;
			}
			// SimpleDataHandler.move(this, dataTypeVal);
		},
		onOk: function(widget) {
			var dialog = this;
			var code = dialog.getContentElement('tab-basic', 'code').getValue();
			var backType = dialog.getContentElement('tab-basic', 'backType').getValue();
			var dataPoints = dialog.getContentElement('tab-basic', 'dataPoints').getValue();

			function doSome(data) {
				setTimeout(function() {
					var target = dialog.getSelectedElement().$;
					var $_target = $(target).find('.h-card');
					if (dialog.insertMode) {
						$_target.attr('id', SimpleDataHandler.id);
					}
					$_target.text(data);
				}, 50);
			}

			function error(xhr, textStatus) {
				doSome('脚本错误');
			}

			if (backType == 'signValue') {
				ck.util.sendAjax({
					url: 'simpleData/getSimpleData',
					data: {
						pageId: pageId,
						code: code
					}
				}, doSome, error);
			} else if (backType == 'table') {
				$_doc.trigger('saveSmartDataCache');
				var inputObj = document.querySelector('#previewTable');
				var rules = new ruleJS('previewTable');
				rules.init();
				var parsed = rules.parse(dataPoints, inputObj);
				var error = parsed.error;
				var result = error ? '-' : parsed.result;
				doSome(result);
			} else if (backType == 'smartParam') {
				SimpleDataHandler.showSmartParam(SimpleDataHandler.id, code, error);
				ck.util.sendAjax({
					url: 'editor/useArgList/getSimpleParamByPageId',
					data: {
						pageId: pageId
					}
				}, function(data) {
					code = SimpleDataHandler.getSmartParamName(code, data);
					doSome(code);
				}, error);
			}
		}
	};
});